package com.bw.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.jwt.JWTUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bw.domain.MaterialUser;
import com.bw.domain.Name;
import com.bw.domain.Order;
import com.bw.service.MaterialUserService;
import com.bw.service.NameService;
import com.bw.service.OrderService;
import com.bw.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    OrderService orderService;


    @Autowired
    NameService nameService;

    @Autowired
    MaterialUserService materialUserService;

    @RequestMapping("login")
    public Result login(@RequestBody MaterialUser materialUser)
    {
        LambdaQueryWrapper<MaterialUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MaterialUser::getUsername,materialUser.getUsername());
        MaterialUser one = materialUserService.getOne(wrapper);
        if(one==null)
        {
            return Result.fail("不存在此用户");
        }
        if(!one.getPwd().equals(materialUser.getPwd()))
        {
            return Result.fail("密码错误");
        }
        String token = JWTUtil.createToken(BeanUtil.beanToMap(one), "123".getBytes());
        return  Result.success(token);
    }



    @RequestMapping("/list")
    public Result getlist()
    {

        List<Order> list= orderService.selectlist();


        return Result.success(list);


    }


    @RequestMapping("/delete")
    private Result getdete(Integer id)
    {
        boolean remove = orderService.removeById(id);

        return Result.success();


    }

    @RequestMapping("/getxlk")
    private Result getxlk()
    {

        List<Name> list = nameService.list();
        return Result.success(list);

    }


    @RequestMapping("/getadd")
    public Result getadd(@RequestBody Order order)
    {
        boolean save = orderService.save(order);

        return Result.success();

    }







}
